package com.inspur.icity.bus.base.core.util;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.inspur.icity.bus.base.core.inspurresult.*;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Created by jn-dinggao on 2017/12/25.
 */
public class InspurApi {

    protected static Logger logger = LoggerFactory.getLogger(InspurApi.class);

    /**
     * 第三方APP申请的用户名
     */
    private static final String APP_USERNAME = "icity";

    /**
     * 第三方APP申请的密码，经过MD5加密
     */
    private static final String APP_PWD = "1dsadusakd2345dsadsadsaio6789";

    private static final String APP_TOKEN = "1MGG9EIMQ2J8OR06HD8NV2952Y8C3BQVGVX6KYH1O6V38U5944M18CX2G626KU1Y";
    /**
     * 主机名称
     */
//    private static final String HOST = "http://htime.inspur.com:5854/app/mock/22/"; //测试使用
//    private static final String HOST = "http://www.icity24.cn/icity-bus"; //济南好运巴士
    private static final String HOST = "http://www.icity24.xyz/icity/transportation/icity"; //石家庄市


    /**
     * 获取token 接口URL
     */
//    private static final String API_TOKEN_URL = HOST + "GET/icity/api/token";
    private static final String API_TOKEN_URL = HOST + "/icity/api/token";

    /**
     * 查询所有线路 接口URL
     */
//    private static final String API_QUERY_ALL_ROUTES = HOST + "POST/icity/api/rest/bus/v1/get-routes"; //测试使用
    private static final String API_QUERY_ALL_ROUTES = "/api/rest/bus/v1/get-routes";


    /**
     * 查询指定起始位置的换乘方案 接口URL
     */
    private static final String API_GET_TRANSFER_SOLUTION = HOST + "POST/icity/api/rest/bus/v1/get-transfer-solution";

    /**
     * 查询指定线路的站点列表 接口URL
     */
//    private static final String API_GET_STATIONS = HOST + "POST/icity/api/rest/bus/v1/get-stations"; //测试使用
    private static final String API_GET_STATIONS = "/api/rest/bus/v1/get-stations"; //测试站点API

    /**
     * 查询指定线路所有车辆的实时位置信息 接口URL
     */
//    private static final String API_GET_BUS_LOCATION = HOST + "POST/icity/api/rest/bus/v1/get-bus-location";
    private static final String API_GET_BUS_LOCATION = "/api/rest/bus/v1/get-bus-location";


    /**
     * 获取token接口
     * @return
     */
    public static TokenResult getToken(){
        String result = HttpRequest.sendGet(API_TOKEN_URL);
        Gson gson = new Gson();
        return gson.fromJson(result, TokenResult.class);
    }

    /**
     * 查询所有线路
     * @param syscode 系统代号
     * @param routeName  线路名称(可选)
     */
    public static BusRouterResult queryAllRoutes(String host,String syscode,String routeName){
        Map params = new HashedMap();
        params.put("token",APP_TOKEN);
        params.put("syscode",syscode);
        if(!StringUtils.isEmpty(routeName)){
            params.put("route_name",routeName);
        }
        String result = HttpRequest.sendPost(host + API_QUERY_ALL_ROUTES, GaodeMapApi.initParams(params));
        System.err.println("路线："+ result);
        BusRouterResult busRouterResult = null;
        try{
            busRouterResult = new Gson().fromJson(result, BusRouterResult.class);
        }catch (Exception e){
            logger.error("定时任务：获取所有线路报错。" + result);
        }
        return busRouterResult;
    }

    /**
     * 查询指定线路的站点列表
     * @param syscode 系统代号
     * @param routeId 线路ID
     * @param upordown 上下行
     */
    public static BusStationResult getStations(String host,String syscode,String routeId,String upordown){
        Map params = new HashedMap();
        params.put("token",APP_TOKEN);
        params.put("syscode",syscode);
        params.put("lineid",routeId);
        params.put("upordown",upordown);
        String result = HttpRequest.sendPost(host + API_GET_STATIONS, GaodeMapApi.initParams(params));
//        System.err.println("站点列表："+ result);
        BusStationResult busStationResult = null;
        try{
            busStationResult = new Gson().fromJson(result, BusStationResult.class);
        }catch (Exception e){
            logger.error("定时任务：获取指定站点报错。" + result);
        }
        return busStationResult;
    }

    /**
     * 查询指定线路中车辆的实时位置信息
     * @param syscode 系统code
     * @param lineid 线路ID
     * @param upordown 上下行
     * @param busid 车辆ID
     */
    public static BusLocationResult getBusLocation(String host,String syscode,String lineid,String upordown,String busid){
        Map params = new HashedMap();
        params.put("token",APP_TOKEN);
        params.put("syscode",syscode);
        params.put("lineid",lineid);
        params.put("upordown",upordown);
        if(!StringUtils.isEmpty(busid)){
            params.put("busid",busid);
        }
        String result = HttpRequest.sendPost(host + API_GET_BUS_LOCATION,GaodeMapApi.initParams(params));
//        System.err.println("实时位置："+ result);
        Gson gson = new Gson();
        BusLocationResult busLocationResult = null;
        try{
            busLocationResult = gson.fromJson(result, BusLocationResult.class);
        }catch (Exception e){
            logger.error("车辆实时位置报错，Gson解析报错！获取数据：" + result);
        }
        return busLocationResult;
    }


    /**
     * 查询指定起始位置的换乘方案
     * @param begin 起点位置
     * @param end 终点位置
     */
    public static TransferSolutionResult getTransferSolution(String begin,String end){
        Map params = new HashedMap();
        params.put("token",APP_TOKEN);
        params.put("begin",begin);
        params.put("end",end);
        String result = HttpRequest.sendGet(API_GET_TRANSFER_SOLUTION);
        Gson gson = new Gson();
        TransferSolutionResult transferSolutionResult = gson.fromJson(result,TransferSolutionResult.class);
        return transferSolutionResult;
    }


    public static void main(String[] args) {
        TokenResult tokenResult =  getToken();
        System.out.println(tokenResult);
      /*  System.out.println(queryAllRoutes("01",""));*/
        System.out.println(getTransferSolution("",""));
        /*System.out.println(getStations("","",""));*/
        /*System.out.println(getBusLocation("","","",""));*/
    }

}
